###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Figure 1b>
#  - April 14, 2016
###############################################

options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))
     
# LOAD BASELINE AND MATCHED DATA
load(paste(path,'replicationPA/Figure1/CA1990_final_data_merged.Rdata',sep=''))
set.seed(57459)
  
# complete building the 1990 dataset 
for(i in 1:58){ 
	dts=matrix(NA,nrow(datas[[i]]),ncol(census))  
	datas[[i]]=cbind(datas[[i]],dts)                        
	datas[[i]][which(!is.na(vec[[i]])),22:31]=census[vec[[i]][which(!is.na(vec[[i]]))],1:10]    
	names(datas[[i]])[22:31]=names(census)
}                          
             
dataFull=datas[[1]]

for(i in 2:58){
	dataFull=rbind(dataFull,datas[[i]])
}
                       

# hispanic MC indicator
hisp92_1=hisp92_2=hisp92_3=hisp92_4=hisp82=array(0,nrow(dataFull))  
 
hisp82[which(dataFull$cd82==30 | dataFull$cd82==34)]=1
hisp82[which(is.na(dataFull$cd82))]=NA
                                      
hisp92_1[which(
	dataFull$cd90==20 |
	dataFull$cd90==26 |
	dataFull$cd90==30 |
	dataFull$cd90==31 |
	dataFull$cd90==33 | 
	dataFull$cd90==34)]=1
#  

hisp92_2[which(
	dataFull$cd90==30 |
	dataFull$cd90==31 |
	dataFull$cd90==33 | 
	dataFull$cd90==34)]=1 
#
  
hisp92_3[which(
	dataFull$cd90==30 |
	dataFull$cd90==31 |
	dataFull$cd90==33 | 
	dataFull$cd90==34 |
	dataFull$cd90==46)]=1 
#

hisp92_4[which(
	dataFull$cd90==20 |
	dataFull$cd90==26 |
	dataFull$cd90==30 |
	dataFull$cd90==31 |
	dataFull$cd90==33 | 
	dataFull$cd90==34 | 
	dataFull$cd90==46)]=1 
#

hisp92_1[which(is.na(dataFull$cd90))]=hisp92_2[which(is.na(dataFull$cd90))]=
	hisp92_3[which(is.na(dataFull$cd90))]=hisp92_4[which(is.na(dataFull$cd90))]=NA
             

Tr1=hisp92_1-hisp82
Tr2=hisp92_2-hisp82
Tr3=hisp92_3-hisp82
Tr4=hisp92_4-hisp82 
       
Tr=Tr1      
Tr[which(hisp82==1)]=NA
      
# data overrun issues again due to mismatched census and vote variables 
hispReg=dataFull$reglatinto
nhispReg=dataFull$totreg-dataFull$reglatinto

total=(dataFull$hispanic+dataFull$nonhisp)
total18=(dataFull$hispanic18+dataFull$nonhisp18)
  
totReg=dataFull$totreg
                                             
yhisp=hispReg/dataFull$hispanic18
yhisp[which(dataFull$hispanic18==0)]=NA
yhisp[which(yhisp>1)]=NA
                     
x=dataFull$hispanic18/total18

set.seed(1005)                   
indxna=which(!is.na(Tr) & !is.na(x)) 

use.matching=F
if(use.matching==T){
	mout=Match(Tr=Tr[indxna],X=x[indxna],ties=F)
} else{
	load(paste(path,'replicationPA/Figure1/mout1990-CA.Rdata',sep='')) 	
}	

tr_mout=Tr[indxna][c(mout$index.treated,mout$index.control)]
yhisp_mout=yhisp[indxna][c(mout$index.treated,mout$index.control)]
                                                                  
pdf(file=paste(path,'replicationPA/Figure1/conditionRegBias1990.pdf',sep=''))
qqplot(yhisp_mout[which(tr_mout==1)],yhisp_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhisp_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhisp_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()
                
png(file=paste(path,'replicationPA/Figure1/conditionRegBias1990.png',sep=''),res=65)
	qqplot(yhisp_mout[which(tr_mout==1)],yhisp_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhisp_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhisp_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
	 rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()

# End Figure1b.R